import {getLocalStorageItem} from "@/utils/storage-helper";
import Format from '@/utils/format'
import fa from "element-ui/src/locale/lang/fa";
export default {

    data() {
        return {
             // 使用自定义方法将日期格式化为字符串
            timerArray: [new Date()- 3600 * 1000 * 24 * 7, new Date()- 3600 * 1000 * 24 * 1], // 初始化为 null 或合适的日期值，
            sumWaySelectList: [{value: '合计'},{value: '分时'}, {value: '分日'}, {value: '分周'}, {value: '分月'}],
            promotionDeviceSelcetList: [{label:'全部',value: ''}, {label:'移动',value: '移动'}, {label:'计算机',value: '计算机'}],
            clickRegionSelcetList: [{value: '整个广告'}, {value: '仅限组件'}],
            checked: false,
            collectionTime: null,
            // 分页参数
            userInfo: getLocalStorageItem('userInfo'),
            tableList:[],
            jumperPage:0,
            isShowChart:true,
            tableListTop5:[],
            value11: '自定义列',

            options11: [{
                value: 1,
                label: '自定义列'
            }, {
                value: 2,
                label: '默认推荐'
            }],
            form: {
                sumType:'消费',
                userId: getLocalStorageItem('userInfo').userId,
                pageSize:20,
                pageNum:1,
                total:0, sumWay: '分日', promotionDevice: ''
            },
            equipId: '', selectedDivision: '',

            isAutoRecord: '',
            isShowDialog: false,
            // 是否折叠
            isToggleStatus: false,
            // 采集数据
            collectionList: [],
            charts: '',
            // 时间选择器配置
            pickerOptions: {
                shortcuts: [
                    {
                        text: '今天',
                        onClick(picker) {
                            const end = new Date()
                            const start = new Date()
                            picker.$emit('pick', [start, end])
                        }
                    },
                    {
                        text: '昨天',
                        onClick(picker) {
                            const end = new Date()
                            const start = new Date()
                            end.setTime(start.getTime() - 3600 * 1000 * 24 * 1)
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 1)
                            picker.$emit('pick', [start, end])
                        }
                    },

                    {
                    text: '最近7天',
                    onClick(picker) {
                        const end = new Date()
                        const start = new Date()
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
                        picker.$emit('pick', [start, end])
                    }
                },
                    {
                        text: '最近14天',
                        onClick(picker) {
                            const end = new Date()
                            const start = new Date()
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 14)
                            picker.$emit('pick', [start, end])
                        }
                    },
                    {
                    text: '最近30天',
                    onClick(picker) {
                        const end = new Date()
                        const start = new Date()
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
                        picker.$emit('pick', [start, end])
                    }
                },
                    {
                        text: '上个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date(end.getFullYear(), end.getMonth() - 1, 1);
                            end.setMonth(end.getMonth(), 0);
                            picker.$emit('pick', [start, end]);
                        },
                    },
                    {
                        text: '本月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date(end.getFullYear(), end.getMonth(), 1);
                            picker.$emit('pick', [start, end]);
                        },
                    },
                    {
                        text: '本周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date(end);
                            start.setDate(start.getDate() - start.getDay());
                            picker.$emit('pick', [start, end]);
                        },
                    },
                    {
                        text: '上周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date(end);
                            start.setDate(start.getDate() - start.getDay() - 7);
                            end.setDate(end.getDate() - end.getDay() - 1);
                            picker.$emit('pick', [start, end]);
                        },
                    },
                    {
                        text: '本季度',
                        onClick(picker) {
                            const end = new Date();
                            const quarterStartMonth = Math.floor(end.getMonth() / 3) * 3;
                            const start = new Date(end.getFullYear(), quarterStartMonth, 1);
                            picker.$emit('pick', [start, end]);
                        },
                    },
                    {
                        text: '上季度',
                        onClick(picker) {
                            const end = new Date();
                            const quarterStartMonth = Math.floor(end.getMonth() / 3) * 3;
                            const start = new Date(end.getFullYear(), quarterStartMonth - 3, 1);
                            end.setFullYear(start.getFullYear(), quarterStartMonth, 0);
                            picker.$emit('pick', [start, end]);
                        },
                    },
                    {
                        text: '本年',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date(end.getFullYear(), 0, 1);
                            picker.$emit('pick', [start, end]);
                        },
                    },
                    {
                        text: '去年',
                        onClick(picker) {
                            const end = new Date(new Date().getFullYear(), 0, 0);
                            const start = new Date(end.getFullYear(), 0, 1);
                            picker.$emit('pick', [start, end]);
                        },
                    },],
                onPick: obj => {
                    this.picerMinDate = new Date(obj.minDate).getTime()
                },
                disabledDate: time => {
                    // if (this.picerMinDate) {
                    //   // 最多可选3个月
                    //   const LIMIT = 90 * 24 * 60 * 60 * 1000
                    //   const maxTime = this.picerMinDate + LIMIT
                    //   const minTime = this.picerMinDate - LIMIT
                    //   return time.getTime() > maxTime || time.getTime() < minTime
                    // }
                }
            },
            // 列表勾选的数据
            selectionList: [],
            // 展示修改 数据说明弹窗
            isShowRemarkDialog: false
        }
    },

    filters: {
        // 时间格式化
        formatTimer(timer) {
            return Format(new Date(timer), 'yyyy-mm-dd')
        },

    },
    computed: {


    },
    mounted() {
        this.$emit('changeActiveIndexs', this.$route.path);
        this.activeIndexs=this.$route.path
    },
    methods: {
        handleTabClick(tab) {

            // 刷新页面
            window.location.reload();
        },
        calculateYAxisInterval(maxValue) {
            // 调整后逻辑
            if (maxValue > 2000) {
                return Math.ceil((maxValue / 7) / 100) * 100; // 除以7再向上取百位数
            } else {
                let interval = Math.ceil(maxValue / 4 / 100) * 100;
                // 防止 interval 小于 100
                return interval < 100 ? 100 : interval; // 确保间隔最小为 100
            }
        },
         replaceNullWithDash(arr) {
    // 确保数组不为空
    if (arr.length === 0) {
        return arr;
    }

    // 获取数组的最后一个对象
    const lastObject = arr[arr.length - 1];

    // 遍历对象的属性
    for (let key in lastObject) {
        if (lastObject.hasOwnProperty(key) && lastObject[key] === null) {
            // 将值为null的属性修改为"-"
            lastObject[key] = "-";
        }
    }

    return arr;
},
        handleCommand(command) {
            console.log('Selected Command:', command);
            switch (command) {
                case 'hour':
                    this.selectedDivision = '小时';
                    break;
                case 'purchase':
                    this.selectedDivision = '购买方式';
                    break;
                case 'device':
                    this.selectedDivision = '推广设备';
                    break;
                case 'province':
                    this.selectedDivision = '分省';
                    break;
                case 'city':
                    this.selectedDivision = '分地市';
                    break;
                case 'customize-column-1':
                    // 处理自定义列 1 的逻辑
                    break;
                case 'customize-column-2':
                    // 处理自定义列 2 的逻辑
                    break;
                default:
                // 处理其他命令的逻辑
            }
        },
        jumpToPage() {
            return
            this.$message('服务器繁忙,请稍后重试！');
        },
        formatDate(date) {
            if (!(date instanceof Date)) {
                date = new Date(date);
            }
            let month = '' + (date.getMonth() + 1),
                day = '' + date.getDate(),
                year = date.getFullYear();

            if (month.length < 2) month = '0' + month;
            if (day.length < 2) day = '0' + day;

            return [year, month, day].join('-');
        },
        // 获取当前的序号
        getIndex(index) {
            let param = index+1
            return param
        },
        // 当前页码改变
        handleCurrentChange(val) {
            console.log('pageNum',val)
            this.form.pageNum = val
            this.queryList()
        },
        // 分页大小改变
        handleSizeChange(val) {
            console.log('pageSize',val)

            this.form.pageSize = val
            this.queryList()
        },
        handleSelectionChange(val) {
            this.selectionList = val
            this.queryList()
        },

    },
}
